package com.ty.zls;

import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.util.QMUIViewHelper;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;
import com.ty.zls.activity.BaseActivity;
import com.ty.zls.activity.LoginActivity;
import com.ty.zls.activity.PresortActivity;
import com.ty.zls.activity.SearchActivity;
import com.ty.zls.adapter.ArreageAdapter;
import com.ty.zls.adapter.HadWaterAdapter;
import com.ty.zls.adapter.NeedWaterAdapter;
import com.ty.zls.bean.Account;
import com.ty.zls.components.MyReceycleView;
import com.ty.zls.fragment.ArreageFragment;
import com.ty.zls.fragment.HadWaterFragment;
import com.ty.zls.fragment.NeedWaterFragment;
import com.ty.zls.fragment.OtherFragment;
import com.ty.zls.listener.ArreageItemListener;
import com.ty.zls.listener.ArreageLoadMoreListener;
import com.ty.zls.listener.ArreageRefreshListener;
import com.ty.zls.listener.HadWaterItemListener;
import com.ty.zls.listener.HadWaterLoadMoreListener;
import com.ty.zls.listener.HadWaterRefreshListener;
import com.ty.zls.listener.NeedWaterItemListener;
import com.ty.zls.listener.NeedWaterLoadMoreListener;
import com.ty.zls.listener.NeedWaterRefreshListener;
import com.ty.zls.util.CommonUtil;
import com.ty.zls.util.HttpUtil;
import com.ty.zls.util.LocationUtil;
import com.ty.zls.util.StrogeUtil;
import com.xuexiang.xaop.annotation.Permission;
import com.xuexiang.xaop.consts.PermissionConsts;
import com.xuexiang.xupdate.XUpdate;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseActivity {

    @ViewInject(R.id.tabs)
    private QMUITabSegment tabs;
    @ViewInject(R.id.contentViewPager)
    private ViewPager contentViewPager;
    @ViewInject(R.id.main_topbar)
    private QMUITopBarLayout main_topbar;

    private Map<ContentPage, View> mPageMap = new HashMap<>();
    private ContentPage mDestPage = ContentPage.Item1;
    private PagerAdapter mPagerAdapter = new PagerAdapter() {
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getCount() {
            return ContentPage.SIZE;
        }

        @Override
        public Object instantiateItem(final ViewGroup container, int position) {
            ContentPage page = ContentPage.getPage(position);
            View view = getPageView(page);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            container.addView(view, params);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    };

    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {
        }

        @Override
        public void onPageSelected(int i) {
            if (i == 0) {
                main_topbar.setTitle("未抄表");
                ((NeedWaterFragment)mPageMap.get(ContentPage.Item1)).init();
            } else if (i == 1) {
                main_topbar.setTitle("已抄表");
                ((HadWaterFragment)mPageMap.get(ContentPage.Item2)).init();
            } else if (i == 2) {
                main_topbar.setTitle("欠费用户");
                ((ArreageFragment)mPageMap.get(ContentPage.Item3)).init();
            } else if (i == 3) {
                main_topbar.setTitle("其它");
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {
        }
    };

    @Override
    @Permission({PermissionConsts.STORAGE,PermissionConsts.LOCATION})
    protected void onCreate(Bundle savedInstanceState) {
        QMUIStatusBarHelper.translucent(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        x.view().inject(this);
        initTopBar();
        inittab();

    }

    /**
     * 初始化标题栏
     */
    private void initTopBar() {
        main_topbar.setTitle("未抄表");
        main_topbar.setBackgroundColor(getResources().getColor(R.color.app_color_theme_7));
    }


    QMUITabSegment.Tab needWater;
    QMUITabSegment.Tab hadwater;
    QMUITabSegment.Tab arreage;
    QMUITabSegment.Tab other;


    /**
     * 初始化tab
     */
    private void inittab() {
        contentViewPager.setAdapter(mPagerAdapter);
        contentViewPager.setCurrentItem(mDestPage.getPosition(), false);
        contentViewPager.addOnPageChangeListener(pageChangeListener);
        tabs.setHasIndicator(false);
        needWater = new QMUITabSegment.Tab(
                ContextCompat.getDrawable(this, R.mipmap.icon_tabbar_component),
                ContextCompat.getDrawable(this, R.mipmap.icon_tabbar_component_selected),
                "未抄表", false
        );
        needWater.setmSignCountDigits(4);
        needWater.setSignCountMargin(0, -QMUIDisplayHelper.dp2px(this, 4));
        needWater.showSignCountView(this, 0);


        hadwater = new QMUITabSegment.Tab(
                ContextCompat.getDrawable(this, R.mipmap.icon_tabbar_util),
                ContextCompat.getDrawable(this, R.mipmap.icon_tabbar_util_selected),
                "已抄表", false
        );
        hadwater.setmSignCountDigits(4);
        hadwater.setSignCountMargin(0, -QMUIDisplayHelper.dp2px(this, 4));
        hadwater.showSignCountView(this, 0);

        arreage = new QMUITabSegment.Tab(
                ContextCompat.getDrawable(this, R.mipmap.icon_tabbar_lab),
                ContextCompat.getDrawable(this, R.mipmap.icon_tabbar_lab_selected),
                "已欠费", false
        );
        arreage.setmSignCountDigits(4);
        arreage.setSignCountMargin(0, -QMUIDisplayHelper.dp2px(this, 4));
        arreage.showSignCountView(this, 0);


        other = new QMUITabSegment.Tab(
                ContextCompat.getDrawable(this, R.mipmap.other),
                ContextCompat.getDrawable(this, R.mipmap.other_select),
                "其它", false
        );

        tabs.addTab(needWater);
        tabs.addTab(hadwater);
        tabs.addTab(arreage);
        tabs.addTab(other);
        tabs.setupWithViewPager(contentViewPager, false);
        tabs.setMode(QMUITabSegment.MODE_FIXED);
        tabs.addOnTabSelectedListener(new QMUITabSegment.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int index) {
            }

            @Override
            public void onTabUnselected(int index) {
            }

            @Override
            public void onTabReselected(int index) {

            }

            @Override
            public void onDoubleTap(int index) {
            }
        });
    }


    private View getPageView(ContentPage page) {
        View view = mPageMap.get(page);
        if (view == null) {
            if (page == ContentPage.Item1) {
                NeedWaterFragment needWaterFragment = new NeedWaterFragment(this,needWater);
                needWaterFragment.init();
                view = needWaterFragment;
            } else if (page == ContentPage.Item2) {
                view = new HadWaterFragment(this,hadwater);
            } else if (page == ContentPage.Item3) {
                view = new ArreageFragment(this,arreage);
            } else if (page == ContentPage.Item4) {
                view = new OtherFragment(this);
            }
            mPageMap.put(page, view);
        }
        return view;
    }


    public enum ContentPage {
        Item1(0),
        Item2(1),
        Item3(2),
        Item4(3);
        public static final int SIZE = 4;
        private final int position;

        ContentPage(int pos) {
            position = pos;
        }

        public static ContentPage getPage(int position) {
            switch (position) {
                case 0:
                    return Item1;
                case 1:
                    return Item2;
                case 2:
                    return Item3;
                case 3:
                    return Item4;
                default:
                    return Item1;
            }
        }

        public int getPosition() {
            return position;
        }
    }
}
